using System;
using System.Windows.Forms;
using ConfigurationSettings;
using ServiceLocator;

namespace FileTracker.Forms
{
    public partial class FormPreferences : Form
    {
        public int SplitterDistance
        {
            get { return this.splitContainer1.SplitterDistance; }
            set { this.splitContainer1.SplitterDistance = value; }
        }

        public FormPreferences()
        {
            InitializeComponent();
        }

        private void FormPreferences_Load(object sender, EventArgs argEventArgs)
        {
            if (this.DesignMode)
            {
                return;
            }

            treeViewMenu.ExpandAll();
            userControlColumnColors1.Edited += new EventHandler(userControlEdited);
            userControlDefaultPath1.Edited += new EventHandler(userControlEdited);
            userControlDirectoryColors1.Edited += new EventHandler(userControlEdited);
            userControlFileColors1.Edited += new EventHandler(userControlEdited);
            userControlGeneral1.Edited += new EventHandler(userControlEdited);
            userControlKeyBinding1.Edited += new EventHandler(userControlEdited);
            userControlOtherColors1.Edited += new EventHandler(userControlEdited);

            userControlColumnColors1.Visible = false;
            userControlDefaultPath1.Visible = false;
            userControlDirectoryColors1.Visible = false;
            userControlFileColors1.Visible = false;
            userControlGeneral1.Visible = false;
            userControlKeyBinding1.Visible = false;
            userControlOtherColors1.Visible = false;

        }

        void userControlEdited(object sender, EventArgs argEventArgs)
        {
            buttonApply.Enabled = true;
            buttonOK.Enabled = true;
        }

        private void treeViewMenu_BeforeSelect(object sender, TreeViewCancelEventArgs argEventArgs)
        {
            userControlColumnColors1.Visible = false;
            userControlDefaultPath1.Visible = false;
            userControlDirectoryColors1.Visible = false;
            userControlFileColors1.Visible = false;
            userControlGeneral1.Visible = false;
            userControlKeyBinding1.Visible = false;
            userControlOtherColors1.Visible = false;
        }

        private void treeViewMenu_AfterSelect(object sender, TreeViewEventArgs argEventArgs)
        {
            switch (argEventArgs.Node.Text)
            {
                case "General":
                    userControlGeneral1.Initialize();
                    userControlGeneral1.Visible = true;
                    break;
                case "Paths":
                    userControlDefaultPath1.Initialize();
                    userControlDefaultPath1.Visible = true;
                    break;
                case "File Colors":
                    userControlFileColors1.Initialize();
                    userControlFileColors1.Visible = true;
                    break;
                case "Directory Colors":
                    userControlDirectoryColors1.Initialize();
                    userControlDirectoryColors1.Visible = true;
                    break;
                case "Column Colors":
                    userControlColumnColors1.Initialize();
                    userControlColumnColors1.Visible = true;
                    break;
                case "Other Colors":
                    userControlOtherColors1.Initialize();
                    userControlOtherColors1.Visible = true;
                    break;
                case "Key Mapping":
                    userControlKeyBinding1.Initialize();
                    userControlKeyBinding1.Visible = true;
                    break;
            }

            //buttonApply_Click(null, null);
        }

        private void FormPreferences_FormClosing(object sender, FormClosingEventArgs argEventArgs)
        {
            if (userControlColumnColors1.DirtyFlag ||
                userControlDefaultPath1.DirtyFlag ||
                userControlDirectoryColors1.DirtyFlag ||
                userControlFileColors1.DirtyFlag ||
                userControlGeneral1.DirtyFlag ||
                userControlKeyBinding1.DirtyFlag ||
                userControlOtherColors1.DirtyFlag)
            {
                if (MessageBox.Show("You have unsaved changes in the current panel. Do you wish to discard your changes?", "Discard changes?", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    argEventArgs.Cancel = true;
                    return;
                }
            }

	        IApplicationSettings localSettings = Locator.Resolve<IApplicationSettings>();

            //ApplicationSettings PreferencesConfig = new ApplicationSettings();
			localSettings.PreferenceWindowLocation = this.Location;
			localSettings.PreferenceWindowSize = this.Size;
			localSettings.PreferencesSplitterDistance = splitContainer1.SplitterDistance;
			localSettings.Save();
        }

        private void buttonApply_Click(object sender, EventArgs argEventArgs)
        {
            userControlColumnColors1.OnApply();
            userControlDefaultPath1.OnApply();
            userControlDirectoryColors1.OnApply();
            userControlFileColors1.OnApply();
            userControlGeneral1.OnApply();
            userControlKeyBinding1.OnApply();
            userControlOtherColors1.OnApply();

            buttonApply.Enabled = false;
        }

        private void buttonCancel_Click(object sender, EventArgs argEventArgs)
        {
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs argEventArgs)
        {
            buttonApply_Click(sender, argEventArgs);
            this.Close();
        }


    }
}